// pages/mandate/mandate.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
	data: {
		userInfo: {
			avatarUrl: defaultAvatarUrl,
			nickName: '',
		},
		hasUserInfo: false,
		canIUseGetUserProfile: false,
	},
	bindViewTap() {
		wx.navigateTo({
			url: '../logs/logs'
		})
	},
	onLoad() {
		if (wx.getUserProfile) {
			this.setData({ canIUseGetUserProfile: true })
		}
	},
	handleAuth() {
		const _this = this
		wx.login({
			success: (res) => {
				_this.getUserInfo(res.code)
			},
		})
	},
	getUserInfo(code) {
		wx.request({
			url: 'http://127.0.0.1:8080/camp/login?code=' + code,
			method: 'post',
			header: {
				"content-type": "application/json"
			},
			success(res) {
				const { data } = res.data
				if (data) {
					wx.setStorageSync('userInfo', data)
					wx.showToast({
						title: '登录成功',
						duration: 800
					})
					setTimeout(() => {
						wx.navigateBack({
							url: '/pages/my/my',
						})
					},1000)
				}
			}
		})
	},
	onback() {
		wx.setStorageSync('userinfo', this.data.userInfo)
		wx.navigateBack({
			url: '/pages/my/my',
		})
	}
})
